package com.mz.ozy.mvp.presenter;

import android.content.Context;

import com.mz.ozy.base.BasePresenterImpl;
import com.mz.ozy.bean.EventBean;
import com.mz.ozy.bean.LoginBean;
import com.mz.ozy.constants.Constants;
import com.mz.ozy.mvp.model.BindPhoneModel;
import com.mz.ozy.mvp.view.BindPhoneContract;
import com.mz.ozy.net.MyObserver;
import com.mz.ozy.utils.AppUtils;
import com.mz.ozy.utils.RxUtil;
import com.mz.ozy.utils.SPUtils;

import org.greenrobot.eventbus.EventBus;

import okhttp3.ResponseBody;


public class BindPhonePresenter extends BasePresenterImpl<BindPhoneContract.View> implements BindPhoneContract.Presenter {

    private BindPhoneModel mModel;

    public BindPhonePresenter() {
        mModel = new BindPhoneModel();
    }

    @Override
    public void onBind(Context context, int type, String mobilePhone, String code, String id) {
        mModel.onBind(context, type, mobilePhone, code, id)
                .compose(RxUtil.rxSchedulerHelper(mView.<ResponseBody>bindToLifecycle()))
                .subscribe(new MyObserver<LoginBean>(mView) {

                    @Override
                    public void onSuccesss(LoginBean loginBean) {
                        AppUtils.saveLoginData(context, loginBean, LoginBean.LOGIN_TYPE_CODE);
                        EventBus.getDefault().post(new EventBean(Constants.EventCode.LOGIN_SUCCESS, null));
                        SPUtils.getInstance(context).putString(Constants.SPName.USER, mobilePhone);
                        mView.onBindSuccess(loginBean);
                    }
                });
    }
}
